from pynput import mouse
from util.configUtil import *
import logging.config
# 多肽实现的各个种族
from race.yehaibo.monk.Monk import *
from race.yehaibo.shaman.Shaman import *
from race.yehaibo.warrior.Warrior import *
from race.yehaibo.thief.Thief import *
from race.yehaibo.paladin.Paladin import *
from race.yehaibo.druid.Druid import *
from race.yehaibo.hunter.Hunter import *
from race.yehaibo.jump.JumpStopOffline import *
from race.yehaibo.dh.Dh import *

threadPoolKeyboard = []
threadPoolMouse = []

# 控制同时只有一个线程可以进入
sem = threading.Semaphore(1)
kb = kController()
ms = mController()

logging.config.fileConfig('./util/logging.conf')
log = logging.getLogger("example")

stillCanCreateNewThread = True
frozenKeyPress = False
frozenMouseScroll = False
gcDuringMousemoveThresholdObj = int(getIni("./util", "memory", "gcDuringMousemoveThreshold"))
gcDuringMousemoveObj = getIni("./util", "memory", "gcDuringMousemove")
race = ''
# 后续用到了多肽
# print("out1:" + str(stillCanCreateNewThread))
player = Player(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb, ms=ms,
                sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui='')
# print("out2:" + str(stillCanCreateNewThread))
# print("out3:" + str(player.stillCanCreateNewThread))

"""
    功能描述：
        1、在界面选择种族后，会通过python的多肽来决定具体执行哪个子类
        2、并且通过构造函数向子类中传递大量的参数，都通过各个职业的子类来进行面向对象的编程
"""


def oo(raceIn, ui, MainWindow):
    global player, race, mouse_listener, keyboard_listener
    # 从界面选下拉框择那个当前职业，也就会执行哪段代码

    # ★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|★★★★★★|
    # print("haihaihaihaihaihaihaihaihaihai" + ui.comboBox.currentText())
    race = raceIn
    if race == '防战':
        player = TankWarrior(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                             threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                             ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '武器战':
        pass
    elif race == '狂暴战':
        pass
    elif race == '酒仙':
        player = TankMonk(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                          threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                          ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '织雾':
        player = HealMonk(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                          threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                          ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '踏风':
        player = DpsMonk(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                         threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                         ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '奶萨':
        player = HealShaman(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                            threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                            ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '增强萨':
        player = EnhanceShaman(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                               threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                               ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '电萨':
        player = ElementShaman(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                               threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                               ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '狂徒贼':
        player = ktThief(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                         threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                         ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '敏锐贼':
        player = mrThief(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                         threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                         ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '奇袭贼':
        pass
    elif race == '防骑':
        player = PaladinTank(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                             threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                             ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '熊德':
        player = DruidTank(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                           threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                           ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '奶德':
        player = HealDruid(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                           threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                           ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '兽王猎':
        player = BeastHunter(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                             threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                             ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == 'tbc贼1':
        player = tbcThief1(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                           threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                           ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)

    elif race == '原地跳':
        player = JumpStopOffline(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                                 threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race,
                                 kb=kb,
                                 ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == '奶骑':
        player = HealPaladin(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                             threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                             ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    elif race == 'DH复仇T':
        player = DhTank(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                        threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                        ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)

    elif race == 'DH浩劫':
        player = DhDPS(stillCanCreateNewThread=stillCanCreateNewThread, frozenKeyPress=frozenKeyPress,
                        threadPoolKeyboard=threadPoolKeyboard, threadPoolMouse=threadPoolMouse, race=race, kb=kb,
                        ms=ms, sem=sem, gcDuringMousemoveObj=gcDuringMousemoveObj, ui=ui)
        player.setMainWindow(MainWindow)
    # # 排评级的全局变量
    # player.stopLoop = False

    # 按照多肽的方式，动态绑定对应职业的类
    stopListen()

    mouse_listener = mouse.Listener(
        on_move=player.on_mouse_move_parent,
        on_click=player.on_mouse_click_parent,
        on_scroll=player.on_mouse_scroll_parent)

    keyboard_listener = keyboard.Listener(
        on_press=player.on_keyboard_press_parent,
        on_release=player.on_keyboard_release_parent)
    player.mouse_listener = mouse_listener
    player.keyboard_listener = keyboard_listener

    startListen()

    log.debug('如果代码异常，会导致仅仅执行父类的方法，子类的方法并不会被执行')
    return player


# 控制鼠标
# mouse = Controller()
# #打印当前鼠标位置
# log.debug('current pointer position {0}'.format(mouse.position))
# #鼠标从当前位置移动单位位置
# mouse.move(-100, -100)
# #按下右键
# mouse.press(Button.right)
# #释放右键
# mouse.release(Button.right)

mouse_listener = mouse.Listener(
    on_move=player.on_mouse_move_parent,
    on_click=player.on_mouse_click_parent,
    on_scroll=player.on_mouse_scroll_parent)


# Collect events until released
# with mouse.Listener(
#         on_move=on_move,
#         on_click=on_click,
#         on_scroll=on_scroll) as listener:
#     listener.join()        #join会阻塞住线程的

# ...or, in a non-blocking fashion:
def startListen():
    startMouseListen()
    startKeyboardListen()
    return


def stopListen():
    stopMouseListen()
    stopKeyboardListen()
    return


def pauseListen(flag):
    global stillCanCreateNewThread, threadPoolKeyboard, threadPoolMouse
    stopListen()
    stillCanCreateNewThread = flag
    log.debug("pause停止线程1")
    for tt in threadPoolKeyboard:
        log.debug("pause停止线程1")
        tt.pause()
    for tt in threadPoolMouse:
        log.debug("pause停止线程1")
        tt.pause()
    player.removeFinishedTrhead(threadPoolKeyboard)
    player.removeFinishedTrhead(threadPoolMouse)

    return


def clearListen(flag):
    log.debug("销毁了1。。。。")
    global stillCanCreateNewThread, threadPoolKeyboard
    stillCanCreateNewThread = flag
    for tt in threadPoolKeyboard:
        log.debug("销毁了2。。。。")
        tt.terminate()
    return


def terminateAllThreadPool():
    # 消耗所有的鼠标，键盘线程
    global threadPoolMouse, threadPoolKeyboard
    for tt in threadPoolKeyboard:
        tt.terminate()
    for tt in threadPoolMouse:
        tt.terminate()
    return


def resumeListen(flag):
    global stillCanCreateNewThread, threadPoolKeyboard
    stillCanCreateNewThread = flag
    for tt in threadPoolKeyboard:
        tt.resume()
    return


def runListen():
    runMouseListen()
    runKeyboardListen()
    return


def startMouseListen():
    mouse_listener.start()
    return


def stopMouseListen():
    mouse_listener.stop()
    return


def runMouseListen():
    mouse_listener.wait()
    return


# 键盘控制
# keyboard = Controller()
# time.sleep(3)
# #输入空格，小写a 大写A，以及小写时按shift+a输出的大写A
# keyboard.press(Key.space)
# keyboard.release(Key.space)
#
# keyboard.press('a')
# keyboard.release('a')
# keyboard.press('A')
# keyboard.release('A')
# with keyboard.pressed(Key.shift):
#     keyboard.press('a')
#     keyboard.release('a')
# #用键盘直接输出字符点击操作
# # Type 'Hello World' using the shortcut type method
# keyboard.type('Hello World')


def freeThreadPool():
    global threadPoolKeyboard, threadPoolMouse
    player.removeFinishedTrhead(threadPoolKeyboard)
    player.removeFinishedTrhead(threadPoolMouse)
    log.debug('在定时器中频繁的释放内存中的键盘、鼠标数组中已经结束的线程。')
    return


keyboard_listener = keyboard.Listener(
    on_press=player.on_keyboard_press_parent,
    on_release=player.on_keyboard_release_parent)


# ...or, in a non-blocking fashion:
def startKeyboardListen():
    keyboard_listener.start()
    return


def stopKeyboardListen():
    keyboard_listener.stop()
    return


def runKeyboardListen():
    keyboard_listener.wait()
    return

# Collect events until released
# with keyboard.Listener(
#         on_press=on_press,
#         on_release=on_release) as listener:
#     listener.join()  # 最后一个，如果不阻塞，这个测试程序就直接结束了
